<template>
  <div class="wutong-pool">
    
  </div>
</template>

<script setup>
import { ref, watch, toRefs, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getProjectList,getTalentList,getUserInfo,companyApplyInfoList,getTztPhone } from '@/api/company.js';
import { login } from '@/api/index.js';

const router = new useRouter();
// localStorage.setItem('token', 'bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJzaW5nbGVMb2dpbiI6MiwiZXhwIjoxNzIwNTE3MDEzLCJhdXRob3JpdGllcyI6WyJpc0FkbWluIl0sImp0aSI6ImRSY1hySmwxX2d3REtPbWZjM3BkN0kwZWxCUSIsImNsaWVudF9pZCI6ImFkbWluIiwidG9rZW4iOiJsb2dpbl90b2tlbl81ODAzODAxNTI4OTkzMDY2MjkifQ.SKWiM9OR1pLqfzGxcO1LoRZSrdak1YXYaIkUEleV8EPYTncbeyCTHMoZIjiVUWWHrgotLNnx3HAkdtxR_k60hVt8z9-UgcCT5T674RHxGUlPcXmOKPxMe8Egc3rdPg5T7rl0M58lzl4E7oQGHTb6LW5MA2YnaXAMBATGTbBUB_VUzOiENIsac9I7JryCtIxyCs4NWlG4zQB5JnA4mMCIuaa5KAcyxZz-xoHYMRfbg_YH3dQiaYySi5T52DHyIDpspEBzFYewJqP0ZRntN3-xbIqMlQ-hA6VHwcLTa2_QiqGB-yNWCsMzLU5DdwrCs2YD9pNIXOlCk1ixvPVgZy05Yw');

const getTicket = () => {
  lightAppJssdk.user.getTicket ({
      success:function(res){ //成功回调
        console.log('getTicket-res:',res);
        if(res == '未登录'){
          taiztLoginApi()
        }else{
          const { data } = JSON.parse(res)
          console.log('getTicket-data:',data);
          let jsonparsedata = JSON.parse(data)
          console.log('jsonparsedata1:',jsonparsedata);
          const { ticket } = jsonparsedata
          console.log('ticket1:',ticket);
          resolveToken(ticket)
        }
      },
      fail:function(res){ //错误返回
        console.log('111fail-res:',res);
    }
  });
}

const taiztLoginApi = () => {
  lightAppJssdk.user.loginapp ({
      success:function(res){ //成功回调
        const { data } = JSON.parse(res)
        console.log('login-api-data:',data);
        let jsonparsedata = JSON.parse(data)
        console.log('jsonparsedata2:',jsonparsedata);
        const { ticket } = jsonparsedata
        console.log('ticket2:',ticket);
        resolveToken(ticket)
      },
      fail:function(res){ //错误返回
        console.log('login-fail-res:',res);
      }
    });
}


const getTicket2 = () => {
  lightAppJssdk.user.getTicket ({
      success:function(res){ //成功回调
        console.log('2getTicket-res:',res);
        if(res == '未登录'){
          taiztLoginApi2()
        }else{
          const { data } = JSON.parse(res)
          console.log('2getTicket2-data:',data);
          let jsonparsedata = JSON.parse(data)
          console.log('2jsonparsedata1:',jsonparsedata);
          const { ticket } = jsonparsedata
          console.log('2ticket1:',ticket);
          getTztmobilePhone(ticket)
        }
      },
      fail:function(res){ //错误返回
        console.log('2fail-res:',res);
    }
  });
}

const taiztLoginApi2 = () => {
  lightAppJssdk.user.loginapp ({
      success:function(res){ //成功回调
        const { data } = JSON.parse(res)
        console.log('2login-api-data:',data);
        let jsonparsedata = JSON.parse(data)
        console.log('2jsonparsedata2:',jsonparsedata);
        const { ticket } = jsonparsedata
        console.log('2ticket2:',ticket);
        getTztmobilePhone(ticket)
      },
      fail:function(res){ //错误返回
        console.log('2login-fail-res:',res);
      }
    });
}


const mobilePhone = ref('');
const getTztmobilePhone= async (ticket) => {
  const { code,msg } = await getTztPhone({
    code: ticket
  });
  console.log('getTztmobilePhone--res-code:',code);
  console.log('getTztmobilePhone--res-msg:',msg);
  if(msg){
    mobilePhone.value = msg
    localStorage.setItem('mobilePhone', mobilePhone.value);
  }else{
    getTicket()
  }
}

//  获取code,获取token
const resolveToken = async (ticket) => {
  const params = {
    account: 'admin',
    password: 'a3420497643ef9a58b6cd83961a9f96c',
    origin: 'password',
    client_id: 'admin',
    client_secret: '123456',
    scope: 'all',
    grant_type: 'taizhoutong',
    code: ticket,
    timestamp: new Date().getTime()
  }
  console.log('params:',params);
  //  登录 获取 token
  const { code, data, msg } = await login(params);
  console.log('code:',code);
  console.log('msg:',msg);
  console.log('data:',data);
  if (code === 200) {
    const token = data.token;
    if (token) {
      console.log('token:',token);
      localStorage.setItem('token', token);
      //有token就有账号，直接进入总览首页
      router.push({ path: '/approvedindex' })
    }
  }
  
  if( (code == 400) && (msg == '无效的账号') ){
    setTimeout(()=>{
      if(mobilePhone.value){
        getApplyInfoList()
      }
    },500)
  }
}

//审核列表
const applyId = ref('')
const getApplyInfoList = async () => {
  const { code, data } = await companyApplyInfoList({
    currentPage: 1,
    pageSize: 20,
    applyPhone: mobilePhone.value,
  });
  if ((code === 200) && data.list.length ) {
    if(data.list[0].auditStatus == '未审核'){
      router.push({ path: '/applicate', query: { status: 0 }})
    }else if(data.list[0].auditStatus == '已审核'){
      router.push({ path: '/applicate', query: { status: 1 }})
    }else if(data.list[0].auditStatus == '退回'){
      applyId.value = data.list[0].applyId
      router.push({ path: '/applicate', query: { status: 2, applyId: applyId.value}})
    }
  }else{
    //没有审核信息，没有申请过
    router.push({ path: '/applicate', query: { status: 3 }})
  }
}
router.push({ path: '/applicate', query: { status: 3 }})
onMounted(() => {
  getTicket()
  getTicket2()
})

</script>

<style lang="scss" scoped>
.wutong-pool {
  box-sizing: border-box;
  height: 100vh;
  background: #fff;
  padding: 3rem 2rem;
}
.header-img{
  width: 100%;
  text-align: center;
}
.logo{
  width: 20rem;
}
.header-text{
  text-align: center;
  font-size: 1.8rem;
  font-family:'微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑', sans-serif;
  font-weight:700;
}
.btn{
  text-align: center;
  margin-top: 5rem;
}
.apply{
  width: 20rem;
  height: 4rem;
  margin: 0 auto;
}
.bohui{
  font-family:'微软雅黑', sans-serif;
  font-weight:400;
  font-style:normal;
  font-size: 1.4rem;
  margin-top: 2rem;
  text-align: center;
}
</style>